<?php
namespace Company\Controller;
use Think\Controller;
use QL\QueryList;//爬虫框架引用

class TestcliController extends Controller {
	//爬虫测试
	 public function querylist(){
		$url=$this->curl_web('http://news.baidu.com/ns?cl=2&rn=50&tn=news&word=%E6%B6%A1%E9%98%B3','http://news.baidu.com');
		$rule=array('title' => array('.c-title a',html),
					'a' => array('.c-title a',href)
					);
        //采集某页面所有的图片
		$data = QueryList::Query($url,$rule)->data;
        //打印结果
		foreach($data as $k=>$v){
			$html="<a href='".$v['a']."' target='_blank'>".$v['title']."</a>";
			echo '<br>'.$html;
		}
        // dump($data);
    }
	public function curl_web($url1,$url2){
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url1); // 要访问的地址           
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		curl_setopt($curl, CURLOPT_REFERER, url2);
		curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie信息的文件名称
		curl_setopt($curl, CURLOPT_COOKIEFILE,$GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		$tmpInfo = curl_exec($curl); // 执行操作
		if (curl_errno($curl)) {
		   echo '操作超时!';
		}
		curl_close($curl); // 关键CURL会话
		return $tmpInfo; // 返回数据
	}
    public function index(){
		file_put_contents('123.txt','123||& '.date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
    }
    protected function rootCheck(){
        if(null==session('agent')){
            $this->redirect('Reg/login');
        }
		$company_status=session('agent.status');
		$childs=session('agent.childs');
		// dump(session());
		// die;
		
		if($company_status<>1){
			$this->error('账号正在审核中，请等待通过后再登录。。。');
		}
		// dump(!$company_status);
		// dump(empty($company_cilids_status));
		// die;
		if(!empty($childs)){
			$company_cilids_status=$childs['status'];
			if($company_cilids_status<>1){
				$this->error('账号正在审核中，请等待通过后再登录。。。');
			}
			$this->assign('childs_company',$childs);
			$g=session('agent.childs');
		}else{
			$g=session('agent');
		}
		// dump($childs);
		// die;
		
        $g=session('agent');
		C('erp_dbname',$g['db']);
		$base_config=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));//加载配置信息
		$b=M('Book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$list=$base_config->getField('en_name,value');
		C('base_config',$list);
		
		//商家编码--begin
		// $importExcelPushorderTempName=C('base_config.importExcelPushorderTempName');
		// if(!empty($importExcelPushorderTempName)){
			// $b_count=$b->count();
			// if($b_count>1){
				// $return['status']=0;
				// $return['message']="多账册请传入账册编码";
				// $this->ajaxReturn($return);
			// }
			// $b_info=$b->find();
			// $merchant_code=$b_info['merchant_code'];//商家编码
			// C('base_config.merchant_code',$merchant_code);
		// }
		//商家编码--end
		
        //////权限检查
        if($g['type']==0) {
            if(session('user_root')!=null){
                $root=session('user_root');
            }else{
                $department_id = $g['department_id'];
                $db_name = $g['db'];
                $m = M('department', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
                $root_group_id = $m->where(array('id' => $department_id))->getField('root_group_id');
                $m = M('root', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
                $group_id = explode(',', $root_group_id);
                $where['id'] = array('in', $group_id);
                $all_root = $m->where($where)->field('controller,action')->select();
                $root = array();
                foreach ($all_root as $v) {
                    if (strpos($v['action'], ',') === false) {
                        $root[$v['controller']][] = $v['action'];
                    } else {
                        $new_root = explode(',', $v['action']);
                        foreach ($new_root as $u) {
                            $root[$v['controller']][] = $u;
                        }
                    }
                }
                unset($all_root);
                session('user_root',$root);
                ///////开始权限判断
            }
			
            //echo "<pre>";
            //var_dump($root);
            $is_root = 0;
            foreach ($root as $k => $v) {
                if ($k == CONTROLLER_NAME && in_array(ACTION_NAME, $v)) {
                    $is_root = 1;
                }
            }
            if ($is_root==0) {
                $no_arr = array(//额外的都具备的权限
                    'Index' => array('index', 'welcome'),
                    'Unit' => array('getUnit','getCoin','getCountry'),
                    'Category' => array('getCategory'),
                    'Role' => array('getRole', 'getEditRole'),
                    'List' => array('warehouse_select_session_delete', 'warehouse_select'),
                    'Product'=>array('getBook','getWarehouse'),
					'Salesman'=>array('goods'),
                );
                foreach($no_arr as $k=>$v){
                    if($k == CONTROLLER_NAME && in_array(ACTION_NAME, $v)){
                        $is_root=1;
                    }
                }
                if($is_root==0){
                    if(IS_AJAX){
                        alert(0,'没有权限');
                    }else{
                        echo '没有权限';
                        exit;
                    }
                }
            }
        }
    }
}